package com.tianqu.tms.www.financia.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.www.financia.actionform.FinanciaCarInputForm;
import com.tianqu.tms.www.financia.viewhelper.FinanciaCarInputViewHelper;
import com.tianqu.tms.www.financia.viewhelper.FinanciaCompanyInputViewHelper;

public class FinanciaCarCreationDispatchAction extends TmsAction {
	private static final String LOADIMG = "imgLoad";// 跳转到图片上传
	private static final String TOSAVE = "toSave";// 保存
	private static final String POSITION = "position";// 图片位置
	private static final String TOCLEAR = "toClear";// 清空填入内容 放入一个标识字段证明进入到确认页面了

	private Logger logger = Logger.getLogger(FinanciaCarCreationDispatchAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		String actionForward = TmsConstants.BLANK;
		// 根据页面按钮选择跳转页面
		FinanciaCarInputForm financiaCompanyForm = (FinanciaCarInputForm) form;	

		FinanciaCarInputViewHelper viewhelp = new FinanciaCarInputViewHelper();

		if (uc.getAttribute(TmsConstants.VIEWHELP) != null) {
			viewhelp = (FinanciaCarInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
		}
		// 给ViewHelper赋值
		BeanUtils.copyProperties(viewhelp, form);
		

		if (!StringUtil.isNullOrBlank(financiaCompanyForm.getImgUpLoad1())) {
			//
			uc.removeAttribute(POSITION);
			uc.setAttribute(POSITION, TmsConstants.ONE);
			actionForward = LOADIMG;
		}
		if (!StringUtil.isNullOrBlank(financiaCompanyForm.getImgUpLoad2())) {
			//
			uc.removeAttribute(POSITION);
			uc.setAttribute(POSITION, TmsConstants.TWO);
			actionForward = LOADIMG;
		}
		if (!StringUtil.isNullOrBlank(financiaCompanyForm.getImgUpLoad3())) {
			//
			uc.removeAttribute(POSITION);
			uc.setAttribute(POSITION, TmsConstants.THREE);
			actionForward = LOADIMG;
		}
		if (!StringUtil.isNullOrBlank(financiaCompanyForm.getImgUpLoad4())) {
			//
			uc.removeAttribute(POSITION);
			uc.setAttribute(POSITION, TmsConstants.FOUR);
			actionForward = LOADIMG;
		}
		if (!StringUtil.isNullOrBlank(financiaCompanyForm.getToSave())) {
			// 确认页面
			uc.removeAttribute(TOCLEAR);
			uc.setAttribute(TOCLEAR, TmsConstants.ONE);
			actionForward = TOSAVE;
		}
		uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);// 将viewhelp放到uc中
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(actionForward);
	}

}
